package com.zhike.b2b.mall.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.springframework.stereotype.Service;

import com.saas.biz.commen.CommGetId;
import com.saas.biz.commen.CommMethodMgr;
import com.saas.biz.dao.ordercartDAO.OrderCartDAO;
import com.saas.biz.dao.ordercartDAO.OrderCartExt;
import com.saas.sys.buffer.Buffers;
import com.saas.sys.exp.SaasApplicationException;
import com.saas.sys.log.Logger;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.dao.Dbtable;

@Service
public class OrderCartInfo extends BaseService
{
  Dbtable tradeQuery = new Dbtable();
  CommMethodMgr comm = new CommMethodMgr();
  Logger log = new Logger(this);
  Buffers inBuffer;
  Buffers outBuffer;
  ArrayList queryResult = new ArrayList();

  public void setTradeQuery(Dbtable paramDbtable)
  {
    this.tradeQuery = paramDbtable;
  }

  public Dbtable getTradeQuery()
  {
    return this.tradeQuery;
  }

  public void setOutBuffer(Buffers paramBuffers)
  {
    this.outBuffer = paramBuffers;
  }

  public Buffers getOutBuffer()
  {
    return this.outBuffer;
  }

  public ArrayList getQueryResult()
  {
    return this.queryResult;
  }

  public void setQueryResult(ArrayList paramArrayList)
  {
    this.queryResult = paramArrayList;
  }

  public void addCart(Buffers paramBuffers)
  {
    int i = -1;
    this.inBuffer = paramBuffers;
    this.outBuffer = paramBuffers;
    String str = new CommGetId().getTradeId("order_list", "trade_id");
    try
    {
      i = addCart(str, paramBuffers);
    }
    catch (Exception localException)
    {
      this.log.LOG_INFO(localException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "成功");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "失败");
    }
  }

  public int addCart(String paramString, Buffers paramBuffers)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VTRADE_ID", paramString);
    localOrderCartExt.setParam(":VCUST_ID", paramBuffers.getString("CUST_ID"));
    localOrderCartExt.setParam(":VSTOCK_ID", paramBuffers.getString("STOCK_ID"));
    localOrderCartExt.setParam(":VSTOCK_TYPE", paramBuffers.getString("STOCK_TYPE"));
    localOrderCartExt.setParam(":VSTOCK_NAME", paramBuffers.getString("STOCK_NAME"));
    localOrderCartExt.setParam(":VCLASS_ID", paramBuffers.getString("CLASS_ID"));
    localOrderCartExt.setParam(":VSTOCK_NUM", paramBuffers.getString("STOCK_NUM"));
    localOrderCartExt.setParam(":VSTOCK_UNIT", paramBuffers.getString("STOCK_UNIT"));
    localOrderCartExt.setParam(":VMONEYTYPE", paramBuffers.getString("MONEYTYPE"));
    localOrderCartExt.setParam(":VSTOCK_PRICE", paramBuffers.getString("STOCK_PRICE"));
    float f1 = Float.parseFloat(paramBuffers.getString("STOCK_NUM"));
    float f2 = Float.parseFloat(paramBuffers.getString("STOCK_PRICE"));
    float f3 = 0.0F;
    f3 = f1 * f2;
    localOrderCartExt.setParam(":VSTOCK_TOTLE_PRICE", Float.valueOf(f3));
    localOrderCartExt.setParam(":VSTATE", "0");
    localOrderCartExt.setParam(":VBUYER_CUST_ID", paramBuffers.getString("BUYER_CUST_ID"));
    localOrderCartExt.setParam(":VOPER_USER", paramBuffers.getString("OPER_USER"));
    String str = isHasRecord(paramBuffers.getString("STOCK_ID"), paramBuffers.getString("BUYER_CUST_ID"));
    if ("0".equals(str))
      this.tradeQuery.executeBy(localOrderCartExt.insBy("INS_CART_ALL"));
    else if ("1".equals(str))
      this.tradeQuery.executeBy(localOrderCartExt.insBy("UPDATE_ORDER_LIST_NUM"));
    return 0;
  }

  public ArrayList getCartInfo(String paramString1, String paramString2)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VCUST_ID", paramString1);
    localOrderCartExt.setParam(":VSTOCK_TYPE", paramString2);
    ArrayList localArrayList = localOrderCartExt.selByList("SEL_GOODS_TO_CART_INFO");
    if (localArrayList != null)
      return localArrayList;
    return null;
  }

  public String getCustName(String paramString)
  {
    String str = "select cust_name from company where cust_id = '" + paramString + "'";
    //jt.queryForObject(str, requiredType)
    return "";
  }

  public String getProductNum(String paramString1, String paramString2, String paramString3)
  {
   /* ArrayList localArrayList = new JDBCExceute().excute("select num from " + paramString1 + " where " + paramString2 + " = '" + paramString3 + "'");
    if ((localArrayList != null) && (localArrayList.size() > 0))
    {
      HashMap localHashMap = (HashMap)localArrayList.get(0);
      if (localHashMap.get("num") != null)
        return localHashMap.get("num").toString();
    }*/
    return "0";
  }

  public String isHasRecord(String paramString1, String paramString2)
  {
    /*ArrayList localArrayList = new JDBCExceute().excute("select stock_id from order_list where stock_id = '" + paramString1 + "' and buyer_cust_id = '" + paramString2 + "' and state = 0");
    if ((localArrayList != null) && (localArrayList.size() > 0))
      return "1";*/
    return "0";
  }

  public ArrayList getCartListByStockBuyerId(String paramString1, String paramString2)
  {
    ArrayList localArrayList = new ArrayList();
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VBUYER_CUST_ID", paramString1);
    localOrderCartExt.setParam(":VSTOCK_ID", paramString2);
    localArrayList = localOrderCartExt.selByList("SEL_CART_BY_BUYER_ID_STOCK_ID");
    return localArrayList;
  }

  public ArrayList getProductByStockId(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VSTOCK_ID", paramString);
    localArrayList = localOrderCartExt.selByList("SEL_PRODUCT_STOCK_ID");
    return localArrayList;
  }

  public void addGoodsToCart(Buffers paramBuffers)
    throws SaasApplicationException
  {
    int i = -1;
    this.inBuffer = paramBuffers;
    this.outBuffer = paramBuffers;
    OrderCartDAO localOrderCartDAO = new OrderCartDAO();
    String str1 = paramBuffers.getString("PRODUCT_ID");
    try
    {
      String str2 = new CommGetId().getTradeId("order_list", "trade_id");
      localOrderCartDAO.setTrade_id(str2);
      localOrderCartDAO.setStock_id(str1);
      localOrderCartDAO.setStock_name(paramBuffers.getString("STOCK_NAME"));
      localOrderCartDAO.setStock_num(paramBuffers.getString("STOCK_NUM"));
      localOrderCartDAO.setStock_unit(paramBuffers.getString("STOCK_UNIT"));
      localOrderCartDAO.setStock_price(paramBuffers.getString("STOCK_PRICE"));
      localOrderCartDAO.setStock_totle_price(paramBuffers.getString("STOCK_TOTLE_PRICE"));
      localOrderCartDAO.setOper_user(paramBuffers.getString("OPER_USER"));
      i = addGoodsToCart(localOrderCartDAO);
    }
    catch (Exception localException)
    {
      this.log.LOG_INFO(localException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ʧ�ܣ�");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ɹ���");
    }
  }

  public int addGoodsToCart(OrderCartDAO paramOrderCartDAO)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VTRADE_ID", paramOrderCartDAO.getTrade_id());
    localOrderCartExt.setParam(":VSTOCK_ID", paramOrderCartDAO.getStock_id());
    localOrderCartExt.setParam(":VSTOCK_NAME", paramOrderCartDAO.getStock_name());
    localOrderCartExt.setParam(":VSTOCK_NUM", paramOrderCartDAO.getStock_num());
    localOrderCartExt.setParam(":VSTOCK_PRICE", paramOrderCartDAO.getStock_price());
    localOrderCartExt.setParam(":VSTOCK_TOTLE_PRICE", paramOrderCartDAO.getStock_totle_price());
    localOrderCartExt.setParam(":VSTOCK_UNIT", paramOrderCartDAO.getStock_unit());
    localOrderCartExt.setParam(":VOPER_USER", paramOrderCartDAO.getOper_user());
    this.tradeQuery.executeBy(localOrderCartExt.insBy("INS_GOODS_TO_CART_INFO"));
    return 0;
  }

  public ArrayList getCartInfoByType(String paramString1, String paramString2, String paramString3)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VBUYER_CUST_ID", paramString1);
    localOrderCartExt.setParam(":VSTOCK_TYPE", paramString2);
    localOrderCartExt.setParam(":VSTATE", paramString3);
    ArrayList localArrayList = localOrderCartExt.selByList("SEL_GOODS_INFO_BY_TYPE_STATE");
    if (localArrayList != null)
      return localArrayList;
    return null;
  }

  public ArrayList getCartInfoGroupByClass(String paramString1, String paramString2, String paramString3)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VBUYER_CUST_ID", paramString1);
    localOrderCartExt.setParam(":VSTOCK_TYPE", paramString2);
    localOrderCartExt.setParam(":VSTATE", paramString3);
    ArrayList localArrayList = localOrderCartExt.selByList("SEL_GOODS_INFO_BY_TYPE_STATE_CLASS");
    if (localArrayList != null)
      return localArrayList;
    return null;
  }

  public ArrayList getCartInfoByOrderId(String paramString)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VORDER_ID", paramString);
    ArrayList localArrayList = localOrderCartExt.selByList("SEL_CART_INFO_BY_ORDER_ID");
    if (localArrayList != null)
      return localArrayList;
    return null;
  }

  public void delCartGoods(Buffers paramBuffers)
    throws SaasApplicationException
  {
    int i = -1;
    this.inBuffer = paramBuffers;
    this.outBuffer = paramBuffers;
    String str1 = paramBuffers.getString("trade_id");
    StringTokenizer localStringTokenizer = new StringTokenizer(str1, ",");
    String str2 = "";
    try
    {
      while (localStringTokenizer.hasMoreTokens())
      {
        str2 = localStringTokenizer.nextToken();
        i = delCartGoods(str2);
      }
    }
    catch (Exception localException)
    {
      this.log.LOG_INFO(localException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ʧ�ܣ�");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ɹ���");
    }
  }

  public int delCartGoods(String paramString)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VTRADE_ID", paramString);
    this.tradeQuery.executeBy(localOrderCartExt.insBy("DEL_GOODS_TO_CART_INFO"));
    return 0;
  }

  public void setCartInfoStockNum(Buffers paramBuffers)
    throws SaasApplicationException
  {
    int i = -1;
    this.inBuffer = paramBuffers;
    this.outBuffer = paramBuffers;
    String str1 = paramBuffers.getString("BATTCH_ID");
    String str2 = paramBuffers.getString("STOCK_NUM");
    String str3 = paramBuffers.getString("STOCK_TOTLE_PRICE");
    try
    {
      StringTokenizer localStringTokenizer1 = new StringTokenizer(str1, "|");
      StringTokenizer localStringTokenizer2 = new StringTokenizer(str2, "|");
      StringTokenizer localStringTokenizer3 = new StringTokenizer(str3, "|");
      String str4 = "";
      String str6 = "";
      while (localStringTokenizer1.hasMoreTokens())
      {
        str4 = localStringTokenizer1.nextToken();
        String str5 = localStringTokenizer2.nextToken();
        str6 = localStringTokenizer3.nextToken();
        i = setCartInfoStockNum(str4, str5, str6);
      }
    }
    catch (SaasApplicationException localSaasApplicationException)
    {
      this.log.LOG_INFO(localSaasApplicationException.getMessage());
    }
    if (i != 0)
    {
      this.outBuffer.setInt("RESULT_CODE", -1);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ʧ�ܣ�");
    }
    else
    {
      this.outBuffer.setInt("RESULT_CODE", 0);
      this.outBuffer.setString("RESULT_INFO", "ҵ����ɹ���");
    }
  }

  public int setCartInfoStockNum(String paramString1, String paramString2, String paramString3)
    throws SaasApplicationException
  {
    OrderCartExt localOrderCartExt = new OrderCartExt();
    localOrderCartExt.setParam(":VTRADE_ID", paramString1);
    localOrderCartExt.setParam(":VSTOCK_NUM", paramString2);
    localOrderCartExt.setParam(":VSTOCK_TOTLE_PRICE", paramString3);
    this.tradeQuery.executeBy(localOrderCartExt.insBy("UPD_CART_INFO_STOCK_NUM_BY_ID"));
    return 0;
  }
}

